package com.hejie.commentservice.controller;

import com.hejie.commentservice.dto.CommentDTO;
import com.hejie.commentservice.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/comments")
@Api(tags = "评论管理接口")
public class CommentController {
    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping
    @ApiOperation("添加评论")
    public ResponseEntity<CommentDTO> addComment(@Valid @RequestBody CommentDTO commentDTO) {
        return new ResponseEntity<>(commentService.addComment(commentDTO), HttpStatus.CREATED);
    }

    @GetMapping("/video/{videoId}")
    @ApiOperation("分页获取视频评论")
    public ResponseEntity<Page<CommentDTO>> getVideoComments(
            @PathVariable Long videoId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ResponseEntity.ok(commentService.getVideoComments(videoId, page, size));
    }

    @GetMapping("/reply/{parentId}")
    @ApiOperation("分页获取评论回复")
    public ResponseEntity<Page<CommentDTO>> getCommentReplies(
            @PathVariable Long parentId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ResponseEntity.ok(commentService.getCommentReplies(parentId, page, size));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取评论详情")
    public ResponseEntity<CommentDTO> getCommentById(@PathVariable Long id) {
        return ResponseEntity.ok(commentService.getCommentById(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("更新评论内容")
    public ResponseEntity<CommentDTO> updateComment(
            @PathVariable Long id,
            @Valid @RequestBody CommentDTO commentDTO) {
        return ResponseEntity.ok(commentService.updateComment(id, commentDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除评论")
    public ResponseEntity<Void> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("分页获取用户评论")
    public ResponseEntity<Page<CommentDTO>> getUserComments(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ResponseEntity.ok(commentService.getUserComments(userId, page, size));
    }

    @PostMapping("/{id}/like")
    @ApiOperation("点赞评论")
    public ResponseEntity<Void> likeComment(@PathVariable Long id) {
        commentService.likeComment(id);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}/like")
    @ApiOperation("取消点赞评论")
    public ResponseEntity<Void> unlikeComment(@PathVariable Long id) {
        commentService.unlikeComment(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/video/{videoId}/count")
    @ApiOperation("统计视频评论总数")
    public ResponseEntity<Long> countVideoComments(@PathVariable Long videoId) {
        return ResponseEntity.ok(commentService.countVideoComments(videoId));
    }

    @PutMapping("/{id}/status")
    @ApiOperation("更新评论状态")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<CommentDTO> updateCommentStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        return ResponseEntity.ok(commentService.updateCommentStatus(id, status));
    }
}